package ca.paulshin.yunatube.services;

import java.util.Locale;

import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import ca.paulshin.yunatube.R;
import ca.paulshin.yunatube.YunaTubeApplication;
import ca.paulshin.yunatube.common.Constants;
import ca.paulshin.yunatube.common.Preference;
import ca.paulshin.yunatube.common.Utils;
import ca.paulshin.yunatube.main.HotNewsActivity;

import com.google.android.gcm.GCMBaseIntentService;

public class GCMIntentServiceRetired extends GCMBaseIntentService {
	public static final String SENDER_ID = Constants.GCM_SENDER_ID;
	private String title, message = null;

	public GCMIntentServiceRetired() {
		super(SENDER_ID);
	}

	public void onReceive(Context context, Intent intent) {
	}

	@Override
	protected void onRegistered(Context context, String registrationId) {
		Log.d("log", "Device registered: regId = " + registrationId);
		// Send the id to 3rd part server
		String GCM_SERVER_URL = YunaTubeApplication.debuggable ? Constants.GCM_SENDER_REGID_TEST_URL : Constants.GCM_SENDER_REGID_URL;
		String setRegIdURL = GCM_SERVER_URL + registrationId;
		try {
			Locale locale = this.getResources().getConfiguration().locale;
			if (locale.equals(Locale.KOREA)) {
				new DefaultHttpClient().execute(new HttpGet(setRegIdURL));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	protected void onUnregistered(Context context, String registrationId) {
		Utils.debug("Device unregistered: regId = " + registrationId);
		// TODO: send the id to 3rd part server
	}

	@Override
	protected void onMessage(Context context, Intent intent) {
		// after receiving a message
		try {
			if (Preference.getBoolean(Constants.NOTIFICATION)) {
				title = intent.getStringExtra("TITLE");
				message = intent.getStringExtra("MESSAGE");
				getGCM();
			}
		} catch (Exception e) {
			if (YunaTubeApplication.debuggable) {
				Utils.debug("[onMessage] Exception : " + e.getMessage());
			}
		}
	}

	public void getGCM() {
		Thread thread = new Thread(new Runnable() {
			public void run() {
				handler.sendEmptyMessage(0);
			}
		});
		thread.start();
	}

	private Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			Context context = getApplicationContext();
			// Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
			// vibrator.vibrate(1000);
			setNotification(context);
			// Util.toast(context, title + "/" + message);
		}
	};

	@Override
	protected void onError(Context context, String errorId) {
		Utils.debug("Received error: " + errorId);
	}

	@Override
	protected boolean onRecoverableError(Context context, String errorId) {
		return super.onRecoverableError(context, errorId);
	}

	private void setNotification(Context context) {
		NotificationManager notificationManager = null;
		Notification notification = null;
		try {
			notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
			notification = new Notification(R.drawable.ic_notification_big, title, System.currentTimeMillis());
			Intent intent = new Intent(context, HotNewsActivity.class);

			intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

			PendingIntent pi = PendingIntent.getActivity(context, 0, intent, 0);
			notification.setLatestEventInfo(context, title, message, pi);
			// Uses the default lighting scheme
			notification.defaults |= Notification.DEFAULT_LIGHTS;
			// Will show lights and make the notification disappear when the presses it
			notification.flags |= Notification.FLAG_AUTO_CANCEL | Notification.FLAG_SHOW_LIGHTS;
			notificationManager.notify(0, notification);
		} catch (Exception e) {
			Utils.debug("[setNotification] Exception : " + e.getMessage());
		}
	}
}